
# coding=utf-8

test_str = "壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾零元零角壹分" #1234567890.01
# test_str = "壹亿元肆分"
# test_str = "壹元"

uint_factor = [100000000, 10000, 1000, 100, 10, 1, 0.1, 0.01]
unit_list = [["亿","乙"],["万","萬"],["仟","千"],["佰","百"],["拾","十"],["圆","元"],["角"],["分"]]
num_list = [
    ["零", "0", "〇"],
    ["壹", "1", "一"],
    ["贰", "2", "二"],
    ["叁", "3", "三"],
    ["肆", "4", "四"],
    ["伍", "5", "五"],
    ["陆", "6", "六"],
    ["柒", "7", "七"],
    ["捌", "8", "八"],
    ["玖", "9", "九"]
]
other_list = ["整","正"]

UNIT_OFFSET = 1
NUM_OFFSET = 2

def ToNum(test_list):
    index = 0
    data = 0.0
    # print(test_list)
    while index < len(test_list) - 1:
        if test_list[index] == (NUM_OFFSET * 100 + 0) and test_list[index + 1] // 100 == NUM_OFFSET:
            index = index + 1
            continue

        if test_list[index] // 100 == NUM_OFFSET and test_list[index + 1] // 100 == UNIT_OFFSET:
            if index == len(test_list) - 2:
                #end
                data = data + (test_list[index] % 100)
            else :
                data = data + (test_list[index] % 100) * uint_factor[test_list[index + 1] % 100]
            index = index + 2
        else :
            return -1
    # print(data)
    return data


def ToFen(test_list):
    index = 0
    data = 0.0
    # print(test_list)
    while index < len(test_list) - 1:
        if test_list[index] == (NUM_OFFSET * 100 + 0) and test_list[index + 1] // 100 == NUM_OFFSET:
            index = index + 1
            continue

        if test_list[index] // 100 == NUM_OFFSET and test_list[index + 1] // 100 == UNIT_OFFSET:
            data = data + (test_list[index] % 100) * uint_factor[test_list[index + 1] % 100]
            index = index + 2
        else :
            return -1
    # print(data)
    return data

def ChToNum(test_str):

    result_raw = []
    for i in test_str:
        find_result = False

        for j in range(len(unit_list)):
            if i in unit_list[j]:
                result_raw.append(UNIT_OFFSET*100 + j)
                find_result = True
                break

        for j in range(len(num_list)):
            if i in num_list[j]:
                result_raw.append(NUM_OFFSET*100 + j)
                find_result = True
                break

        for j in range(len(other_list)):
            if other_list[j] == i:
                find_result = True
                break

        if not find_result:
            return -1

    # print(result_raw)

    result_yi = []
    result_wan = []
    result_yuan = []
    result_fen = []

    next_find = 0

    #find yi
    for i in range(next_find, len(result_raw)):
        if result_raw[i] == (UNIT_OFFSET*100 + 0):
            result_yi = result_raw[:i + 1]
            next_find = i + 1
            break

    #find wan
    for i in range(next_find, len(result_raw)):
        if result_raw[i] == (UNIT_OFFSET*100 + 1):
            result_wan = result_raw[next_find : i + 1]
            next_find = i + 1
            break

    #find yuan
    for i in range(next_find, len(result_raw)):
        if result_raw[i] == (UNIT_OFFSET*100 + 5):
            result_yuan = result_raw[next_find : i + 1]
            next_find = i + 1
            break

    if len(result_yuan) < 1:
        return -2

    if next_find < len(result_raw) - 1:
        result_fen = result_raw[next_find:]

    if len(result_yi) == 1:
        return -3

    if len(result_wan) == 1:
        return -4

    if len(result_fen) == 1:
        return -5

    yi = ToNum(result_yi)
    if yi < 0 :
        return -6

    wan = ToNum(result_wan)
    if wan < 0 :
        return -7

    yuan = ToNum(result_yuan)
    if yuan < 0 :
        return -8

    fen = ToFen(result_fen)
    if fen < 0 :
        return -9

    return yi * 100000000 + wan * 10000 + yuan + fen

if __name__=='__main__':
    mssg_file_name = '1.txt'
    new_file_name = '1.csv'

    mssg_file = open(mssg_file_name,"r", encoding='utf-8')
    new_file = open(new_file_name,"w", encoding='utf-8')
    for line in mssg_file.readlines():
        s = "".join(line.split())
        write_str = u""
        data = ChToNum(s)
        if s != '':
            if data < 0:
                write_str = u", 错误%d,\n"%(-data)
            else:
                write_str = u", %.2f,\n"%data
        else :
            write_str = u", ,\n"
        new_file.write(s)
        new_file.write(write_str)

    mssg_file.close()
    new_file.close()
